const fs = require('fs');
const path = require('path');
const { parse } = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;

// 读取Vue组件文件内容
const filePath = path.join(__dirname, 'App.vue'); // 替换为实际文件路径
const code = fs.readFileSync(filePath, 'utf8');

// 提取<script>标签内容
const scriptMatch = code.match(/<script>([\s\S]*?)<\/script>/);
const scriptContent = scriptMatch ? scriptMatch[1].trim() : '';

if (!scriptContent) {
  console.error('未找到<script>标签内容');
  process.exit(1);
}

// 解析JavaScript代码为AST
const ast = parse(scriptContent, {
  sourceType: 'module',
  plugins: ['vue', 'objectRestSpread']
});

// 要查找的属性路径，可自定义为任意深度的路径
const propertyPath = 'oParams.message.age.required';

// const propertyPath = 'oParams.oCapbilities';
const pathParts = propertyPath.split('.');

let foundValue = null;

// 遍历AST查找data函数
traverse(ast, {
  ObjectMethod(path) {
    if (path.node.key.name === 'data' && path.node.type === 'ObjectMethod') {
      const dataFunctionBody = path.node.body;
      
      // 查找data函数中的return语句
      const returnStatement = dataFunctionBody.body.find(
        stmt => stmt.type === 'ReturnStatement'
      );
      
      if (returnStatement && returnStatement.argument.type === 'ObjectExpression') {
        // 开始递归查找属性路径
        foundValue = findNestedProperty(returnStatement.argument, pathParts, path.scope);
      }
    }
  }
});

// 递归查找嵌套属性
function findNestedProperty(currentNode, remainingPath, scope) {
  if (remainingPath.length === 0 || currentNode === null) {
    return currentNode;
  }
  
  const currentKey = remainingPath[0];
  
  // 处理对象表达式
  if (currentNode.type === 'ObjectExpression') {
    const property = currentNode.properties.find(
      prop => prop.key && 
             (prop.key.name === currentKey || prop.key.value === currentKey)
    );
    
    if (property) {
      return findNestedProperty(property.value, remainingPath.slice(1), scope);
    }
  }
  // 处理变量引用
  else if (currentNode.type === 'Identifier') {
    const binding = scope.getBinding(currentNode.name);
    if (binding && binding.path.node.init) {
      return findNestedProperty(binding.path.node.init, remainingPath, scope);
    }
  }
  
  return null;
}

// 将节点转换为字符串表示
function getNodeValueAsString(node, scope) {
  if (!node) return null;
  
  switch (node.type) {
    case 'BooleanLiteral':
    case 'StringLiteral':
    case 'NumericLiteral':
      return JSON.stringify(node.value);
      
    case 'Identifier':
      // 尝试解析变量引用
      const binding = scope.getBinding(node.name);
      if (binding && binding.path.node.init) {
        return getNodeValueAsString(binding.path.node.init, scope);
      }
      return node.name;
      
    case 'BinaryExpression':
    case 'LogicalExpression':
    case 'CallExpression':
    case 'MemberExpression':
    case 'ConditionalExpression':
      // 复杂表达式，使用Babel生成代码
      return generate(node).code;
      
    default:
      return `[无法解析的类型: ${node.type}]`;
  }
}

// 输出结果
const valueAsString = getNodeValueAsString(foundValue);
if (valueAsString !== null) {
  console.log(`${propertyPath} 的初始值是: ${valueAsString}`);
} else {
  console.log(`未找到 ${propertyPath} 的定义`);
}